package com.xb.loan.cashier.biz.pay.bankcomm;

import com.xb.loan.cashier.biz.pay.bankcomm.config.BankcommConfig;
import com.xb.loan.cashier.biz.pay.bankcomm.util.BankcommCore;
import com.xb.loan.cashier.biz.pay.bankcomm.util.BankcommSubmit;
import com.xb.loan.cashier.biz.util.XMLUtil;
import com.xb.loan.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class BankcommPayment {
	
	private static Logger log = LoggerFactory.getLogger(BankcommPayment.class);

	/**
	 * 请求接口
	 * @param apiURL
	 * @param params 有序参数
	 * @return 返回验签后数据
	 */
	public static Map<String, Object> apiRequest(String apiURL, Map<String, LinkedHashMap<String, Object>> params){
		Map<String, Object> xmlResMap = null;
		String reqSrc = XMLUtil.mapToXml(params, BankcommConst.ENCODING_GB2312);
		log.debug("apiRequest reqSrc="+reqSrc);
		String reqSign= BankcommCore.attachedSign(reqSrc);
		try {
			reqSign = java.net.URLEncoder.encode(reqSign, BankcommConst.ENCODING_GBK);
			String resSign = BankcommSubmit.post(BankcommConfig.getConfig().getHttpsType(), apiURL, "reqData="+reqSign);
			log.debug("apiRequest resSign="+resSign);
			byte [] resSrc = BankcommCore.attachedVerify(resSign);
			if (resSrc==null) {
				log.info("apiRequest 验签失败");
				return xmlResMap;
			}
			xmlResMap = XMLUtil.parse(resSrc);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return xmlResMap;
	}
	
	/**
	 *  查询订单
	 * @param apiURL
	 * @param merchNo
	 * @param orderId
	 * @return
	 */
	public static Map<String, Object> queryOrder(String apiURL, String merchNo, String orderId){
		Map<String, LinkedHashMap<String, Object>> params = new HashMap<String, LinkedHashMap<String, Object>>();
  		LinkedHashMap<String, Object> headMap = new LinkedHashMap<String, Object>();
  		LinkedHashMap<String, Object> bodyMap = new LinkedHashMap<String, Object>();
  		params.put(BankcommConst.HEAD, headMap);
  		params.put(BankcommConst.BODY, bodyMap);
  		headMap.put("TranCode", BankcommConst.TRAN_CODE_B2C_QUERY);
  		headMap.put("MerchNo", merchNo);
  		bodyMap.put("OrderNos", orderId);
  		log.debug("queryOrder params="+params.toString());
        Map<String, Object> xmlResMap = BankcommPayment.apiRequest(apiURL, params);
        return xmlResMap;
	}
	
	/**
	 * 手机支付提交表单
	 * @param merID
	 * @param orderId
	 * @param orderDate
	 * @param mobileURL
	 * @return
	 */
	public static String mobilePaySubForm(String merID, String orderId, String orderDate, String mobileURL){
		Map<String, String> submitFromData = new HashMap<String, String>();
		submitFromData.put("merchNo", merID);
		submitFromData.put("orderNo", orderId);
		submitFromData.put("orderDate", orderDate);
		String html = BankcommSubmit.createAutoFormHtml(mobileURL, submitFromData,BankcommConst.ENCODING_GBK);//MobileURL
		return html;
	}
	
	/**
	 * 实名认证(客户名称认证没有certType,certNo)
	 * @param url
	 * @param cardNo
	 * @param merchNo
	 * @param custName
	 * @param certType (15居民身份证;16临时身份证;17军人身份证件;18武警身份证件;19港澳台居民通行证;20护照;21其他;22临时户口;23户口簿;24边境证)
	 * @param certNo
	 * @return
	 */
	public static Map<String, Object> realAuth(String url, String merchNo,
                                               String cardNo, String custName, String certType, String certNo) {
		Map<String, LinkedHashMap<String, Object>> params = new HashMap<String, LinkedHashMap<String, Object>>();
		LinkedHashMap<String, Object> headMap = new LinkedHashMap<String, Object>();
  		LinkedHashMap<String, Object> bodyMap = new LinkedHashMap<String, Object>();
  		params.put(BankcommConst.HEAD, headMap);
  		params.put(BankcommConst.BODY, bodyMap);
  		headMap.put("TranCode", BankcommConst.TRAN_CODE_READ_AUTH);
  		headMap.put("MerchNo", merchNo);
  		bodyMap.put("CardNo", cardNo);
  		bodyMap.put("CustName", custName);
		if (StringUtils.isNotEmpty(certType) && StringUtils.isNotEmpty(certNo)) {
			bodyMap.put("CertType", certType);
			bodyMap.put("CertNo", certNo);
		}
		Map<String, Object> xmlResMap = apiRequest(url, params);
		return xmlResMap;
	}
	
	/**
	 * 充值对账
	 * @param apiURL
	 * @param merchNo
	 * @param settleDate
	 * @return
	 */
	public static Map<String, Object> chargeAccount(String apiURL, String merchNo, String settleDate){
		Map<String, LinkedHashMap<String, Object>> params = new HashMap<String, LinkedHashMap<String, Object>>();
  		LinkedHashMap<String, Object> headMap = new LinkedHashMap<String, Object>();
  		LinkedHashMap<String, Object> bodyMap = new LinkedHashMap<String, Object>();
  		params.put(BankcommConst.HEAD, headMap);
  		params.put(BankcommConst.BODY, bodyMap);
  		headMap.put("TranCode", BankcommConst.TRAN_CODE_ACCOUNT);
  		headMap.put("MerchNo", merchNo);
  		bodyMap.put("SettleDate", settleDate);
  		log.debug("account params="+params.toString());
        Map<String, Object> xmlResMap = BankcommPayment.apiRequest(apiURL, params);
        return xmlResMap;
	}
}
